package cn.lili.modules.order.cart.render.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.lili.common.enums.PromotionTypeEnum;
import cn.lili.modules.order.cart.entity.dto.TradeDTO;
import cn.lili.modules.order.cart.entity.enums.RenderStepEnums;
import cn.lili.modules.order.cart.entity.vo.CartSkuVO;
import cn.lili.modules.order.cart.entity.vo.CartVO;
import cn.lili.modules.order.cart.render.CartRenderStep;
import cn.lili.modules.promotion.entity.dos.KanjiaActivityGoods;
import cn.lili.modules.promotion.entity.dos.PointsGoods;
import cn.lili.modules.promotion.entity.dos.RedPackGoods;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 佣金计算
 *
 * @author Chopper
 * @see CartVO
 */
@Service
@RequiredArgsConstructor
public class CommissionRender implements CartRenderStep {


    @Override
    public RenderStepEnums step() {
        return RenderStepEnums.PLATFORM_COMMISSION;
    }

    @Override
    public void render(TradeDTO tradeDTO) {
        buildCartPrice(tradeDTO);
    }

    /**
     * 购物车佣金计算
     *
     * @param tradeDTO 购物车展示信息
     */
    public void buildCartPrice(TradeDTO tradeDTO) {
        //购物车列表
        List<CartVO> cartVOS = tradeDTO.getCartList();

        //计算购物车价格
        for (CartVO cart : cartVOS) {
            //累加价格
            for (CartSkuVO cartSkuVO : cart.getCheckedSkuList()) {
                //PS：砍价、积分商品会有一个额外的结算金额字段来设置结算金额，所以这里特殊判定时会覆盖结算金额
                specialOrderResetSettlement(cartSkuVO);
            }
        }
    }

    /**
     * 特殊订单结算金额处理
     *
     * @param cartSkuVO 购物车商品
     */
    private void specialOrderResetSettlement(CartSkuVO cartSkuVO) {

        if (cartSkuVO.getPromotionTypeEnum() == null) {
            return;
        }
        //如果积分订单 积分订单，单独操作订单结算金额和商家结算字段
        if (cartSkuVO.getPromotionTypeEnum().equals(PromotionTypeEnum.POINTS_GOODS)) {
            Optional<Map.Entry<String, Object>> pointsPromotions =
                    cartSkuVO.getPromotionMap().entrySet().stream().filter(i -> i.getKey().contains(PromotionTypeEnum.POINTS_GOODS.name())).findFirst();
            if (pointsPromotions.isPresent()) {
                JSONObject promotionsObj = JSONUtil.parseObj(pointsPromotions.get().getValue());
                RedPackGoods redPackGoods = JSONUtil.toBean(promotionsObj, RedPackGoods.class);
                cartSkuVO.getPriceDetailDTO().setSpecialOrderSettlementPrice(redPackGoods.getSettlementPrice());
            }
        }
        //如果红包订单 红包订单，单独操作订单结算金额和商家结算字段
        if (cartSkuVO.getPromotionTypeEnum().equals(PromotionTypeEnum.RED_PACK_GOODS)) {
            Optional<Map.Entry<String, Object>> pointsPromotions =
                    cartSkuVO.getPromotionMap().entrySet().stream().filter(i -> i.getKey().contains(PromotionTypeEnum.RED_PACK_GOODS.name())).findFirst();
            if (pointsPromotions.isPresent()) {
                JSONObject promotionsObj = JSONUtil.parseObj(pointsPromotions.get().getValue());
                RedPackGoods redPackGoods = JSONUtil.toBean(promotionsObj, RedPackGoods.class);
                cartSkuVO.getPriceDetailDTO().setSpecialOrderSettlementPrice(redPackGoods.getSettlementPrice());
            }
        }
        //如果砍价订单 计算金额，单独操作订单结算金额和商家结算字段
        if (cartSkuVO.getPromotionTypeEnum().equals(PromotionTypeEnum.KANJIA)) {
            Optional<Map.Entry<String, Object>> kanjiaPromotions =
                    cartSkuVO.getPromotionMap().entrySet().stream().filter(i -> i.getKey().contains(PromotionTypeEnum.KANJIA.name())).findFirst();
            if (kanjiaPromotions.isPresent()) {
                JSONObject promotionsObj = JSONUtil.parseObj(kanjiaPromotions.get().getValue());
                KanjiaActivityGoods kanjiaActivityGoods = JSONUtil.toBean(promotionsObj, KanjiaActivityGoods.class);
                cartSkuVO.getPriceDetailDTO().setSpecialOrderSettlementPrice(kanjiaActivityGoods.getSettlementPrice());
            }
        }
    }


}
